﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLogic.DTO;
using BusinessLogic.Repository_Logic.Interfaces;
using TestSystem.Domain.Abstract;
using TestSystem.Domain.Entities.Candidate;
using TestSystem.Domain.Entities.Position;
using TestSystem.Domain.Repositories;

namespace BusinessLogic.Repository_Logic.Concentrate
{
    public class CandidateRepositoryLogic : ICandidateRepositoryLogic
    {
        private readonly IPositionRepository _positionRepository = new PositionRepository();
        private readonly IAccountRepository _accountRepository = new AccountRepository();
        private readonly ICandidateRepository _candidateRepository = new CandidateRepository(); 

        public CandidateDTO GetEmptyCandidateDTO()
        {
            return new CandidateDTO
                       {
                           Candidate = new Candidate(),
                           //Positions = _positionRepository.Positions.ToList(),
                           Users = _accountRepository.Users.ToList()
                       };
        }

        public IEnumerable<Position> GetAvaiablePositionForUser(int userId)
        {
            List<Position> avaiable = new List<Position>();
            var candidates = _candidateRepository.Candidates.Where(can => can.UserId.Equals(userId));
            foreach (var position in _positionRepository.Positions)
            {
                if(candidates.FirstOrDefault(can => can.PositionID.Equals(position.PositionID)) == null)
                    avaiable.Add(position);
            }
            return avaiable;
        }

        public void AddCandidate(CandidateDTO candidateDto)
        {
            _candidateRepository.AddCandidate(candidateDto.Candidate);
        }

        public IEnumerable<Candidate> GetCandidates()
        {
            return _candidateRepository.Candidates.ToList();
        }

        public void RemoveCandidate(int CandidateId)
        {
            _candidateRepository.RemoveCandidate(_candidateRepository.Candidates.First(can => can.CandidateId.Equals(CandidateId)));
        }

        public int GetCandidatesAmount()
        {
            return _candidateRepository.Candidates.Count();
        }
    }
}
